package com.dz51.core.validate.intercepter;

import java.io.PrintWriter;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.dz51.core.json.ViewResult;
import com.dz51.core.validate.annotation.ParamValidater;
import com.dz51.core.validate.context.UrlMethodValidateMapping;
import com.dz51.core.validate.validator.handler.DefaultValidateHandler;
import com.dz51.core.validate.vo.ValidateResult;

public class ValidateIntercepter extends HandlerInterceptorAdapter {

	private com.dz51.core.validate.validator.handler.DefaultValidateHandler validateHandler = new DefaultValidateHandler();

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		if (!(handler instanceof HandlerMethod)) {
			return super.preHandle(request, response, handler);
		}
		HandlerMethod handlerMethod = (HandlerMethod) handler;
		Method method = handlerMethod.getMethod();
		if (method == null) {
			return super.preHandle(request, response, handler);
		}

		// 获取校验规则
		ParamValidater[] rules = UrlMethodValidateMapping.getInstance().getMappingRule(method);
		// 参数为空，不进行校验
		if (rules == null || rules.length == 0) {
			super.preHandle(request, response, handler);
		}

		// 对参数的校验
		// ValidateRequestWrapper requestWrapper = new
		// ValidateRequestWrapper(request);

		// 进行校验，获取校验结果
		ValidateResult validateResult = validateHandler.handleValidate(request, rules);

		// 判断校验校验结果
		if (validateResult != null && validateResult.isSuccess()) {// 如果校验成功
			if (validateResult.getRequest() != null) {
				return super.preHandle(validateResult.getRequest(), response, handler);
			} else {
				return super.preHandle(request, response, handler);
			}
		} else {// 校验失败,返回原因
			if (request.getRequestURL().toString().indexOf(".json") > -1) {// 以json结尾
				response.setCharacterEncoding("utf-8");
				PrintWriter writer = response.getWriter();
				ViewResult r = ViewResult.newInstance();
				writer.write(r.fail(validateResult.getErrorMsg()).json());
			}
			return false;
		}
	}

	// //对参数的校验
	// ValidateRequestWrapper requestWrapper = new
	// ValidateRequestWrapper(request);
	//
	// //进行校验，获取校验结果
	// ValidateResult validateResult =
	// validateHandler.handleValidate(requestWrapper, rules);
	//
	// //判断校验校验结果
	// if(validateResult != null && validateResult.isSuccess()){//如果校验成功
	// if(validateResult.getRequest() != null){
	// return super.preHandle(validateResult.getRequest(), response, handler);
	// }else{
	// return super.preHandle(request, response, handler);
	// }
	// }else{//校验失败,返回原因
	// if(request.getRequestURL().toString().indexOf(".json")>-1) {//以json结尾
	// response.setCharacterEncoding("utf-8");
	// PrintWriter writer = response.getWriter();
	// Result r = new Result();
	// r.setSuccess(false);
	// r.setMsg(validateResult.getErrorMsg());
	// writer.write(JSON.toJSONString(r));
	// }
	// return false;
	// }
}